package com.hzz.web.boss.report;

import com.hzz.api.area.IAreaService;
import com.hzz.api.report.IReportEventService;
import com.hzz.api.river.IRiverInfoService;
import com.hzz.api.user.IOrgInfoService;
import com.hzz.api.user.IUserService;
import com.hzz.dto.area.AreaDto;
import com.hzz.dto.report.ReportEventDto;
import com.hzz.dto.report.ReportEventFinishDto;
import com.hzz.dto.report.ReportEventSupervisionDto;
import com.hzz.dto.report.ReportEventTreatmentDto;
import com.hzz.dto.river.RiverInfoDto;
import com.hzz.dto.user.OrgInfoDto;
import com.hzz.dto.user.UserDto;
import com.hzz.framework.PageUtil;
import com.hzz.framework.Response;
import com.hzz.framework.annotation.ALogOperation;
import com.hzz.framework.annotation.RfUser2Bean;
import com.hzz.framework.annotation.auth.Logical;
import com.hzz.framework.annotation.auth.RequiresPermissions;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.util.ValidatorUtil;
import com.hzz.framework.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "事件上报")
@RestController
@Slf4j
public class ReportEventController extends BaseController {
    @Autowired
    IReportEventService reportEventService;
    @Autowired
    IAreaService areaService;
    @Autowired
    IOrgInfoService orgInfoService;
    @Autowired
    IUserService userService;
    @Autowired
    IRiverInfoService riverInfoService;
    static final String acPrefix = "/boss/report/reportEvent/";

    @RequiresPermissions(value = {"reportEventPatrol:menu", "reportEventComplaint:menu",}, logical = Logical.OR)
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "page/{pageNum}")
    // @ApiOperation(value = "信息分页(未删除)")
    public Response page(@ModelAttribute ReportEventDto dto, @PathVariable("pageNum") Integer pageNum) {
        log.info("page.........");
        Response result = new Response();
        try {
            dto.setPageSize(CommonConstant.PAGEROW_DEFAULT_COUNT);
            dto.setPageNum(pageNum);
            dto.setDelFlag(0);
            if (dto.getReportingType() == null) {
                return Response.error("上报类型必传!");
            }
            result.data = PageUtil.copy(reportEventService.findDataIsPage(dto));
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    @RequiresPermissions(value = {"reportEventPatrol:info", "reportEventComplaint:info",}, logical = Logical.OR)
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "info/{id}")
    // @ApiOperation(value = "信息详情")
    public Response info(@PathVariable("id") Long id) {
        log.info("info.........");
        Response result = new Response();
        try {
            ReportEventDto dto = new ReportEventDto();
            dto.setId(id);
            dto.setDelFlag(0);
//            String reportingType = request.getParameter("reportingType");
//            if (!ValidatorUtil.notEmpty(reportingType)) {
//                return Response.error("上报类型必传!");
//            }
//            dto.setReportingType(Integer.parseInt(reportingType));
            result.data = reportEventService.findDataById(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    @RequiresPermissions("reportEventComplaint:del")
    @RequestMapping(method = RequestMethod.POST, value = acPrefix + "complaint/del/{id}")
    @ALogOperation(type = "删除", desc = "公众投诉-逻辑删除")
    public Response complaintDel(@PathVariable("id") Long id) {
        log.info("complaintDel.........");
        Response result = new Response(0, "success");
        try {
            ReportEventDto dto = new ReportEventDto();
            dto.setId(id);
            dto.setReportingType(1);
            result.message = reportEventService.deleteDataById(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    @RequiresPermissions(value = {"reportEventPatrol:follow", "reportEventComplaint:follow",}, logical = Logical.OR)
    @RequestMapping(method = RequestMethod.POST, value = acPrefix + "follow/{id}")
    @ALogOperation(type = "关注", desc = "巡查事件")
    // @ApiOperation(value = "逻辑删除")
    public Response follow(@PathVariable("id") Long id) {
        log.info("follow.........");
        Response result = new Response(0, "success");
        try {
            ReportEventDto dto = new ReportEventDto();
            dto.setId(id);
            result.message = reportEventService.follow(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    @RequiresPermissions(value = {"reportEventPatrol:edit", "reportEventComplaint:edit",}, logical = Logical.OR)
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.PUT}, value = acPrefix + "supervision")
    @RfUser2Bean
    @ALogOperation(type = "督办", desc = "巡查事件")
    @ApiOperation(value = "督办")
    public Response supervision(@ModelAttribute ReportEventSupervisionDto dto) {
        log.info("supervision.........");
        Response result = new Response(0, "success");
        try {
            result = reportEventService.supervision(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    @RequiresPermissions(value = {"reportEventPatrol:edit", "reportEventComplaint:edit",}, logical = Logical.OR)
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.PUT}, value = acPrefix + "treatment")
    @RfUser2Bean
    @ALogOperation(type = "事件处理", desc = "巡查事件")
    @ApiOperation(value = "事件处理")
    public Response treatment(@ModelAttribute ReportEventTreatmentDto dto) {
        log.info("treatment.........");
        Response result = new Response(0, "success");
        try {
            if (dto.getTreatmentType() == null) {
                return Response.error("处理方式不能为空");
            }
            //1 事件处理(分配处理)
            //  处理人角色
            //  选择行政区划
            if (ValidatorUtil.notEmpty(dto.getTreatmentAreaId())) {
                AreaDto area = areaService.findDataById(Long.parseLong(dto.getTreatmentAreaId()));
                if (area != null) {
                    dto.setTreatmentAreaName(area.getName());
                }
            }
            // 选择部门
            if (dto.getTreatmentOrgId() != null) {
                OrgInfoDto orgInfoDto = new OrgInfoDto();
                orgInfoDto.setId(dto.getTreatmentOrgId());
                orgInfoDto = orgInfoService.findDataById(orgInfoDto);
                if (orgInfoDto != null) {
                    dto.setTreatmentOrgName(orgInfoDto.getName());
                }
            }
            //  选择处理人员
            if (dto.getTreatmentUserId() != null) {
                UserDto userDto = new UserDto();
                userDto.setId(dto.getTreatmentUserId());
                userDto = userService.findDataById(userDto);
                if (userDto != null) {
                    dto.setTreatmentUserName(userDto.getName());
                }
            }
            dto.setState(1);
            if (dto.getTreatmentType() != 0) {
                //  选择处理河段
                if (dto.getTreatmentReachId() != null) {
                    RiverInfoDto riverInfoDto = new RiverInfoDto();
                    riverInfoDto.setId(dto.getTreatmentReachId());
                    riverInfoDto = riverInfoService.findDataById(riverInfoDto);
                    if (riverInfoDto != null) {
                        dto.setTreatmentReachName(riverInfoDto.getName());
                    }
                }
            }
            result = reportEventService.treatment(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }
//
//    @RequiresPermissions(value = {"reportEventPatrol:edit", "reportEventComplaint:edit",}, logical = Logical.OR)
//    @RequestMapping(method = {RequestMethod.POST, RequestMethod.PUT}, value = acPrefix + "finish")
//    @RfUser2Bean
//    @ALogOperation(type = "事件完结", desc = "巡查事件")
//    @ApiOperation(value = "事件完结")
//    public Response finish(@ModelAttribute ReportEventFinishDto dto) {
//        log.info("finish.........");
//        Response result = new Response(0, "success");
//        try {
//            dto.setState(2);
//            result = reportEventService.finish(dto);
//        } catch (Exception e) {
//            result = Response.error(e.getMessage());
//        }
//        return result;
//    }
}
